﻿using System;
using System.Windows;
using Shoozla.ViewModel;
using System.Collections.Generic;
using MahApps.Metro.Controls;
using System.Windows.Controls;
using System.Threading;

namespace Shoozla.View
{
  
    /// <summary>
    /// Interaction logic for ProgressBarView.xaml
    /// </summary>
    public partial class ProgressBarView : MetroWindow
    {

        private ProgressBarViewModel viewModel;
      
        private Timer updateTimer;

        public ProgressBarView()
        {
            InitializeComponent();         

            viewModel = new ProgressBarViewModel(this,App.MainView);
            DataContext = viewModel;
            viewModel.RequestClose += (s, e) => this.Close(); 
            TimerCallback updateCallback = new TimerCallback(updateUIMethod); 
            updateTimer = new Timer(updateCallback, null, 0, 300);          

        }

        private void updateUIMethod(object o) {
            if (Dispatcher.CheckAccess())
                updateMethod();
            else
                Dispatcher.BeginInvoke((Action)updateMethod);
           
        }

        private void updateMethod()
        {
            if (mainPB.Value != viewModel.Current)
                mainPB.Value = viewModel.Current;
            if (currentTB.Text != "" + viewModel.Current)
                currentTB.Text = "" + viewModel.Current;
            if (descriptionTB.Text != viewModel.Description)
                descriptionTB.Text = viewModel.Description;
        }
    }
}
